package com.zret.oasys.operation.emp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.zret.oasys.dao.DeptDao;
import com.zret.oasys.dao.impl.DeptDaoImpl;
import com.zret.oasys.dbtools.PageData;
import com.zret.oasys.entity.Dept;
import com.zret.oasys.entity.Emp;
import com.zret.oasys.json.entity.GetTableDataJson;
import com.zret.oasys.operation.dept.GetDepts;

/**
 * 
 * @author L.lawliet 通过姓名关键字模糊查找员工
 */
public class GetEmpsByName implements EmpServletOperation {

	@Override
	public void doOperation(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置json编码
		response.setContentType("application/json;charset=utf-8");
		// 2.从jsp获取empname,获取查询到的list
		String name = request.getParameter("empname");
		// 获取page参数
		int page = Integer.valueOf(request.getParameter("page"));
		// 获取pagesize参数
		int pagesize = Integer.valueOf(request.getParameter("limit"));
		// 如果是空值查询到所有员工
		if ("".equals(name)) {
			PageData<Emp> pd = empService.getEmpsByPage(page, pagesize);
			List<Emp> list = pd.getData();
			// 3.创建输出流
			PrintWriter out = response.getWriter();
			// 4.创建Gson对象
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
			// 5.将查询到的员工list转为字符串
			GetTableDataJson<Emp> getEmpsJson = new GetTableDataJson<Emp>(0, "", pd.getTotalCount(), list);
			String jsonString = gson.toJson(getEmpsJson);
			// 6.控制台测试打印
			System.out.println(jsonString);
			// 7.传输给jsp页面
			out.print(jsonString);
			// 8.关闭out
			out.close();
		} else {
			// 模糊查询员工的方法
			// 获取page参数
			int page1 = Integer.valueOf(request.getParameter("page"));
			// 获取pagesize参数
			int pagesize1 = Integer.valueOf(request.getParameter("limit"));
			PageData<Emp> pd = empService.getEmpsByPage(page, pagesize);
			List<Emp> list = empService.getEmpsByName(name);
			// 3.创建输出流
			PrintWriter out = response.getWriter();
			// 4.创建Gson对象
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
			// 5.将查询到的员工list转为字符串
			String jsonString = gson.toJson(list);
			// 6.控制台测试打印
			System.out.println(jsonString);
			// 7.传输给jsp页面
			out.print(jsonString);
			// 8.关闭out
			out.close();
		}
	}
}
